﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dapper.Moon
{
    /// <summary>
    /// sqlserver 特有功能 通过扩展实现
    /// </summary>
    public static class SqlServerExtention
    {
        public static int SqlServerBulkInsert<T>(this IInsertable<T> insertable, DataTable table)
        {
            if (!(insertable is SqlServerInsertable<T>))
            {
                throw new Exception("sqlserver specific functions");
            }
            return (insertable as SqlServerInsertable<T>).BulkInsert(table);
        }

        public static async Task<int> SqlServerBulkInsertAsync<T>(this IInsertable<T> insertable, DataTable table)
        {
            if (!(insertable is SqlServerInsertable<T>))
            {
                throw new Exception("sqlserver specific functions");
            }
            return await (insertable as SqlServerInsertable<T>).BulkInsertAsync(table);
        }

        #region sqlserver 特有功能 ROW_NUMBER分页
        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T> SqlServerRowNumberForPaging<T>(this IQueryable<T> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2> SqlServerRowNumberForPaging<T, T2>(this IQueryable<T, T2> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2 , T3> SqlServerRowNumberForPaging<T, T2, T3>(this IQueryable<T, T2, T3> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4> SqlServerRowNumberForPaging<T, T2, T3, T4>(this IQueryable<T, T2, T3, T4> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5> SqlServerRowNumberForPaging<T, T2, T3, T4, T5>(this IQueryable<T, T2, T3, T4, T5> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5, T6> SqlServerRowNumberForPaging<T, T2, T3, T4, T5, T6>(this IQueryable<T, T2, T3, T4, T5, T6> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5, T6>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5, T6>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5, T6, T7> SqlServerRowNumberForPaging<T, T2, T3, T4, T5, T6, T7>(this IQueryable<T, T2, T3, T4, T5, T6, T7> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5, T6, T7>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5, T6, T7>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5, T6, T7, T8> SqlServerRowNumberForPaging<T, T2, T3, T4, T5, T6, T7, T8>(this IQueryable<T, T2, T3, T4, T5, T6, T7, T8> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> SqlServerRowNumberForPaging<T, T2, T3, T4, T5, T6, T7, T8, T9>(this IQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>).IsUseRowNumberForPaging = true;
            return queryable;
        }

        /// <summary>
        /// sqlserver 特有功能 ROW_NUMBER分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <returns></returns>
        public static IQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> SqlServerRowNumberForPaging<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this IQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> queryable) where T : class
        {
            if (!(queryable is SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>))
            {
                throw new Exception("sqlserver specific functions");
            }
            (queryable as SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>).IsUseRowNumberForPaging = true;
            return queryable;
        }
        #endregion sqlserver 特有功能 ROW_NUMBER分页
    }
}
